/**
 * @brief   : CWELD 数据结构
 * @version : 0.1
 * @author  : wanghuan
 * @date    : 2024.11.01
 * @copyright Copyright (c) 2024 by 神工仿真, All Rights Reserved.
 */

#pragma once

#pragma once
#include "DataStructure/Common/DataStructureNamespace.h"
#include "DataStructure/Common/Id.h"
#include "DataStructure/Common/Real.h"
#include "DataStructure/Input/Element/ElementBase.h"

SG_DATASTRUCTURE_FEM_NAMESPACE_OPEN

/**
 * @brief   : 单元点和，或面连接选项
 * @author  : wanghuan
 * @date    : 2024.11.01
 */
enum CWELDFormats
{
    FORMAT_GridID    = 0,   ///< surface to surface
    FORMAT_ALign     = 1,   ///< point to point
    FORMAT_ElemID    = 2,   ///< element surface or point to element surface
    FORMAT_ElPat     = 9,   ///< surface to surface
    FORMAT_PartPat   = 10,  ///< surface to surface
    FORMAT_GridIDGBI = 100  ///< ???
};

enum SurfaceType
{
    Type_Null = 0,
    Type_Tri  = 1,
    Type_Quad = 2
};

inline std::size_t CountValidElements (const SG::DataStructure::Common::Id* const arr) 
{
    // 从前往后找第一个0的位置
    for (int i = 0; i < 8; ++i) {
        if (arr[i] == 0) {
            return i; // 返回第一个0的索引，即有效元素数量
        }
    }
    return 8; // 全为有效元素
}

struct DECLSPEC CWELDData : public ElementBase
{
    CWELDFormats GetFormat () const
    {
        return static_cast<CWELDFormats> (m_format);
    }
    /**
     * @brief   : 获取当Format为GridID时的SPType
     * @author  : qiulin
     * @date    : 2024.11.07
     * @param   isA [in]  true 返回A端面类型
     *                    false 返回B端面类型
     */
    SurfaceType GetSPType (bool isA)
    {
        if (isA)
        {
            return static_cast<SurfaceType> ((m_type >> 16) & 0xFFFF);
        }
        else
        {
            return static_cast<SurfaceType> (m_type & 0xFFFF);
        }
    }

    /// @brief FORMAT_GridID 格式获得A面的节点个数
    std::size_t GetGUpperSize () const
    {
        return CountValidElements (m_GUpper);
    }

    std::size_t GetLowerSize () const
    {
        return CountValidElements (m_GLower);
    }

    SG::DataStructure::Common::Id   m_pId       = 0;        ///< property Id
    SG::DataStructure::Common::Id   m_Gs        = 0;        ///< 焊接节点id
    SG::DataStructure::Common::I64  m_format    = 0;        ///< 连接形式
    SG::DataStructure::Common::Id   m_g[2]      = { 0 };    ///< 连接节点
    SG::DataStructure::Common::I64  m_type      = 0;        ///< GridID指定面选项
    SG::DataStructure::Common::Id   m_mCId      = -1;        ///< 单元刚度方向
    SG::DataStructure::Common::Id   m_GUpper[8] = { 0 };    ///< GridID上表面节点
    SG::DataStructure::Common::Id   m_GLower[8] = { 0 };    ///< GridID下表面节点
    SG::DataStructure::Common::Id   m_ShIdA     = 0;        ///< 壳A的id
    SG::DataStructure::Common::Id   m_ShIdB     = 0;        ///< 壳B的id
    SG::DataStructure::Common::Real m_XYZ[3]    = { 0.0 };  ///< 自定义朝向
    SG::DataStructure::Common::Id   m_PIdA      = 0;        ///< 用户定义A面的壳属性编号
    SG::DataStructure::Common::Id   m_PIdB      = 0;        ///< 用户定义B面的壳属性编号
    SG::DataStructure::Common::Real m_tavg      = 0.0;
    SG::DataStructure::Common::Real m_tmin      = 0.0;
    SG::DataStructure::Common::Id   m_domainId  = 0;
};

SG_DATASTRUCTURE_FEM_NAMESPACE_CLOSE